package kr.co.bettersoft.pushchat.fragment;

import kr.co.bettersoft.pushchat.activity.R;
import kr.co.bettersoft.pushchat.activity.SettingsAutoLoginActivity;
import kr.co.bettersoft.pushchat.activity.SettingsPasswordActivity;
import kr.co.bettersoft.pushchat.activity.SettingsProfileActivity;
import kr.co.bettersoft.pushchat.activity.SettingsSettingActivity;
import kr.co.bettersoft.pushchat.activity.myWebView;
import kr.co.bettersoft.pushchat.common.CommonConstant;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * SettingsFragment
 *  더보기
 *   프로필 / 비밀번호 / 자동로그인 / 설정 
 *   공지사항 / 메일보내기
 *   
 *   각 버튼을 눌러 상세 화면으로 이동.
 * @author blue
 *
 */
public class SettingsFragment extends Fragment {
	String TAG = "SettingsFragment";

	// 웹뷰용 인텐트
	Intent webIntent;

	TextView titleBar;		// 타이틀
	// 메뉴 목록	
	ImageButton see_more_1_profile;
	ImageButton see_more_2_password_setting;
	ImageButton see_more_3_autologin_setting;
	ImageButton see_more_4_settings;
	ImageButton see_more_5_notification;
	ImageButton see_more_6_send_mail;



	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 핸들러
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			try{
				Bundle b = msg.getData();		
				if(b.getInt("showToast")==1){		// 화면에 토스트 띄우기
					Toast.makeText(getActivity(), b.getString("msg"), Toast.LENGTH_SHORT).show();
				}
				if(b.getInt("showAlert")==1){					 // 경고창 . 
					//
					new AlertDialog.Builder(getActivity())
					.setTitle(CommonConstant.alertTitle)							
					.setMessage(b.getString("msg"))
					.setIcon(R.drawable.ic_dialog_img)		// 경고창 어플 아이콘으로바꿈.
					.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							// 그냥 사용자 확인 용이기 때문에 추가 조작 없음.
						}})
						.setNegativeButton("", null).show();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	};
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_settings, null);

		titleBar = (TextView) view.findViewById(R.id.title_bar);
		see_more_1_profile = (ImageButton) view.findViewById(R.id.see_more_1_profile);
		see_more_2_password_setting = (ImageButton) view.findViewById(R.id.see_more_2_password_setting);
		see_more_3_autologin_setting = (ImageButton) view.findViewById(R.id.see_more_3_autologin_setting);
		see_more_4_settings = (ImageButton) view.findViewById(R.id.see_more_4_settings);
		see_more_5_notification = (ImageButton) view.findViewById(R.id.see_more_5_notification);
		see_more_6_send_mail = (ImageButton) view.findViewById(R.id.see_more_6_send_mail);

		// Button listener
		see_more_1_profile.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), SettingsProfileActivity.class);
				startActivity(intent);
			}
		});
		see_more_2_password_setting.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), SettingsPasswordActivity.class);
				startActivity(intent);
			}
		});
		see_more_3_autologin_setting.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), SettingsAutoLoginActivity.class);
				startActivity(intent);
			}
		});
		see_more_4_settings.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), SettingsSettingActivity.class);
				startActivity(intent);
			}
		});
		see_more_5_notification.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 공지 사항
				Intent webIntent = new Intent(getActivity(), myWebView.class);
				//				webIntent.putExtra("loadingURL", CommonConstant.noticeURL);	
				webIntent.putExtra("loadingURL", CommonConstant.qnaURL);	
				//				startActivity(webIntent);

				// 알림 설정
				//				Intent PrefPushOnOffIntent = new Intent(getActivity(), MainSettingsAutoLoginActivity.class);
				//				startActivity(PrefPushOnOffIntent);
			}
		});
		see_more_6_send_mail.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 미구현
				//				Log.d(TAG,"메일보내기");

				//				Intent PushListIntent = new Intent(getActivity(), MainNoticeActivity.class);
				//				startActivity(PushListIntent);
			}
		});
		return view;
	}


	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/*
	 * 핸들러를 통한 작업.
	 */
	// 화면에 토스트 띄움 msg 변수에 내용을 넣어서 호출하면 화면에 토스트를 띄운다.		// --> 통신 에러시에만.
	/**
	 * showMsg
	 *   화면에 토스트 띄움 msg 변수에 내용을 넣어서 호출하면 화면에 토스트를 띄운다
	 *
	 * @param msg
	 * @param 
	 * @return 
	 */
	public void showMsg(final String msg){			
		new Thread(
				new Runnable(){
					public void run(){
						Message message = handler.obtainMessage();				
						Bundle b = new Bundle();
						b.putInt("showToast", 1);
						b.putString("msg", msg);
						message.setData(b);
						handler.sendMessage(message);
					}
				}
		).start();
	} 
	/**
	 * showResultDialog
	 *  얼럿을 띄운다.
	 *
	 * @param msg
	 * @param 
	 * @return 
	 */
	public void showResultDialog(final String msg){
		new Thread(
				new Runnable(){
					public void run(){
						Message message = handler.obtainMessage();				
						Bundle b = new Bundle();
						b.putInt("showAlert", 1);
						b.putString("msg", msg);			// 화면에 보여줄 메시지
						message.setData(b);
						handler.sendMessage(message);
					}
				}
		).start();
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * onResume
	 *  액티비티 리쥼시 닫기 카운터초기화한다
	 *
	 * @param
	 * @param
	 * @return
	 */
	@Override
	public void onResume(){
		super.onResume();
		FragmentHome.app_end = 0;
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}